function e(e) {
    var t = [];
    for (var a in e) t.push(encodeURIComponent(a) + "=" + encodeURIComponent(e[a]));
    return t.join("&");
}

function t(e) {
    wx.login({
        success: function(t) {
            t.code ? wx.request({
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method: "POST",
                url: e.globalData.url + "user/NomalUser/NomalUserLogin",
                data: {
                    code: t.code
                },
                success: function(t) {
                    if (500 == t.responseCode) {
                        var a = getCurrentPages(), o = a[a.length - 1].route;
                        "pages/home/home" === o || "pages/start/start" === o || wx.redirectTo({
                            url: "/pages/start/start"
                        });
                    }
                    200 == e.responseCode ?(t.globalData.userId = e.data.user.userId, 
                        t.globalData.token = e.data.data.token, t.globalData.userInfo = e.data.data.user) : wx.showToast({
                        title: "请登录",
                        icon: "none"
                    });
                }
            }) : console.log("获取用户登录态失败！" + t.errMsg);
        }
    });
}

var a = function(e) {
    return (e = e.toString())[1] ? e : "0" + e;
};

module.exports = {
    json2Form: e,
    wxpay: function(t, a, o, n) {
        wx.request({
            url: t.globalData.url + "wx/pay/info",
            data: e({
                token: t.globalData.token,
                orderId: a,
                couponId: o,
                payType: 2
            }),
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            success: function(o) {
                if (o.data.state) {
                    var n = o.data.data;
                    wx.requestPayment({
                        timeStamp: n.timeStamp,
                        nonceStr: n.nonceStr,
                        package: n.packageValue,
                        signType: n.signType,
                        paySign: n.paySign,
                        success: function(e) {
                            wx.redirectTo({
                                url: "/pages/service/pay-success/index?status=1&&orderId=" + a
                            });
                        },
                        fail: function(o) {
                            "requestPayment:fail cancel" == o.errMsg && wx.request({
                                url: t.globalData.url + "wx/pay/cancel",
                                data: e({
                                    token: t.globalData.token,
                                    payId: n.payId
                                }),
                                header: {
                                    "Content-Type": "application/x-www-form-urlencoded"
                                },
                                method: "POST",
                                success: function(e) {},
                                fail: function(e) {}
                            }), wx.redirectTo({
                                url: "/pages/service/pay-success/index?status=0&&orderId=" + a
                            });
                        }
                    });
                } else wx.showToast({
                    title: "发起支付失败:" + o.data.message
                });
            }
        });
    },
    formatTime: function(e) {
        var t = e.getFullYear(), o = e.getMonth() + 1, n = e.getDate(), r = e.getHours(), s = e.getMinutes(), c = e.getSeconds();
        return [ t, o, n ].map(a).join("/") + " " + [ r, s, c ].map(a).join(":");
    },
    registerUser: function(e, t, a, o) {
        wx.login({
            success: function(n) {
                var r = n.code;
                e && wx.request({
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    url: a.globalData.url + "user/NomalUser/NomalUserRegister",
                    data: {
                        code: r,
                        encryptedData: e,
                        iv: t
                    },
                    success: function(e) {
                        wx.login({
                            success: function(e) {
                                console.log(e.code)
                                e.code ? wx.request({
                                    header: {
                                        "Content-Type": "application/x-www-form-urlencoded"
                                    },
                                    method: "post",
                                    url: a.globalData.url + "user/NomalUser/NomalUserLogin",
                                    data: {
                                        code: e.code
                                    },
                                    success: function(e) {
                                        e.data.data.responseCode ? (a.globalData.userId = e.data.data.user.userId, a.globalData.token = e.data.data.token, 
                                        a.globalData.userInfo = e.data.data.user, wx.setStorageSync("UserRegister", !0), o(e.data.data)) : wx.showToast({
                                            title: "请登陆",
                                            icon: "none"
                                        });
                                          wx.switchTab({
                                 url: "/pages/home/home"
                                });
                                    }
                                    
                                }) : console.log("获取用户登录态失败！" + e.errMsg);
                            }
                        });
                    }
                });
            }
        });
    },
    checkLogin: t,
    convertMath: function(e) {
        var t = parseFloat(e);
        if (isNaN(t)) return !1;
        var a = (t = Math.round(10 * e) / 1e3).toString(), o = a.indexOf(".");
        for (o < 0 && (o = a.length, a += "."); a.length <= o + 2; ) a += "0";
        return a;
    },
    request: function(a, o, n, r, s, c) {
        wx.showLoading({
            title: "加载中",
            mask: !0
        }), wx.request({
            url: a.globalData.url + o,
            method: n,
            header: {
                "content-type": "POST" == n ? "application/x-www-form-urlencoded" : "application/json"
            },
            data: "POST" == n ? e(r) : r,
            success: function(e) {
                "wx/cust/getOneQueueByShopId" == o || "wx/cust/deletequeue" == o || "wx/cust/deletequeue" == o ? (s(e), 
                wx.hideLoading()) : e.data.state ? (wx.hideLoading(), s(e.data)) : (wx.hideLoading(), 
                "110" === e.data.code ? t(a) : s(e.data));
            },
            fail: function(e) {
                c(e.data), setTimeout(function() {
                    wx.hideLoading(), wx.showToast({
                        title: e.errMsg,
                        icon: "none"
                    });
                }, 1e3);
            }
        });
    },
    transformatLat: function(e, t, a) {
        wx.request({
            url: "https://apis.map.qq.com/ws/coord/v1/translate",
            method: "GET",
            header: {
                "content-type": "application/json"
            },
            data: e,
            success: function(e) {
                t(e.data);
            },
            fail: function(e) {
                a(e.data);
            }
        });
    },
    registerPhone: function(e, t, a, o) {
        wx.login({
            success: function(n) {
                var r = n.code;
                e && wx.request({
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    url: a.globalData.url + "wx/cust/registerPhone",
                    data: {
                        code: r,
                        encryptedData: e,
                        iv: t,
                        token: a.globalData.token
                    },
                    success: function(e) {
                        o(e.data);
                    }
                });
            }
        });
    },
    formatSeconds: function(e) {
        var t = "";
        if (e > 0) {
            var a = Math.floor(e / 60), o = Math.floor(e - 60 * a);
            t = (a < 10 ? "0" + a : a) + ":" + (o < 10 ? "0" + o : o) + '"';
        }
        return t;
    }
};